// Sound Resource
// (c) jimon game studio

#ifndef JEH_JESOUNDRESOURCE
#define JEH_JESOUNDRESOURCE

#include "IResourceManager.h"
#include "ISoundManager.h"

#ifdef JE_SOUND

namespace je
{
	namespace media
	{
		//! Sound Resource
		class jeSoundResource:public je::media::IResource
		{
		protected:
			sound::ISound * Sound;

		public:
			//! Constructor
			jeSoundResource(const jeStringc & ResourceName)
				:IResource(ResourceName),Sound(NULL)
			{
			}

			//! Constructor
			jeSoundResource(const jeStringc & ResourceName,sound::ISound * NewSound)
				:IResource(ResourceName,false),Sound(NewSound)
			{
				Sound->Grab();
			}

			//! Destructor
			~jeSoundResource()
			{
				if(Sound)
					sound::GetSoundManager()->GetRootGroup()->RemoveSound(Sound);
			}

			//! Get Sound
			sound::ISound * GetSound()
			{
				return Sound;
			}

			//! Load Resource
			void Load()
			{
				#ifdef JE_RESOURCES_SOUND_USEBLOBBUFFER
				core::IDataBase_Query * Query = GetResourceManager()->GetDataBase()->Query(jeStringc("SELECT ROWID,* FROM SoundResources WHERE Name = '")+CurrentResourceName+jeStringc("'"));
				u1 CopyToMemory = Query->GetFieldu1("CopyToMemory",false);
				s32 RowId = Query->GetFields32(0,0);
				Query->Drop();

				core::IBuffer * Buffer = GetResourceManager()->GetDataBase()->GetBlobBuffer("SoundResources","Data",RowId);
				Sound = sound::GetSoundManager()->CreateSound(Buffer,NULL,CopyToMemory);
				Buffer->Drop();

				#else
				core::IDataBase_Query * Query = GetResourceManager()->GetDataBase()->Query(jeStringc("SELECT * FROM SoundResources WHERE Name = '")+CurrentResourceName+jeStringc("'"));
				u1 CopyToMemory = Query->GetFieldu1("CopyToMemory",false);

				core::IBuffer * Buffer = Query->GetFieldBuffer("Data");
				Sound = sound::GetSoundManager()->CreateSound(Buffer,NULL,CopyToMemory);
				Buffer->Drop();

				Query->Drop();
				#endif
			}

			//! UnLoad Resource
			void UnLoad()
			{
				sound::GetSoundManager()->GetRootGroup()->RemoveSound(Sound);
				Sound = NULL;
			}

			//! Get Type Name
			virtual jeStringc GetTypeName()
			{
				return jeStringc("SoundResource");
			}
		};

		//! Sound Resource Factory
		class jeSoundResourceFactory:public je::media::IResourceFactory
		{
		protected:
			typedef jeMap<jeStringc,jeSoundResource*> SoundMapType;
			SoundMapType SoundMap;
		public:

			//! Constructor
			jeSoundResourceFactory()
			{
			}

			//! Destructor
			~jeSoundResourceFactory()
			{
				ClearResources();
			}

			//! Add Resources
			void AddResources(IResource * Resource)
			{
				SoundMap.set(Resource->GetName(),static_cast<jeSoundResource*>(Resource));
				Resource->Grab();
			}

			//! Load Resources
			void LoadResources()
			{
				ClearResources();

				core::IDataBase_Query * Query = GetResourceManager()->GetDataBase()->Query("SELECT * FROM SoundResources");
				while(!Query->Eof())
				{
					jeStringc ResourceName = Query->GetFieldjeStringc("Name");
					jeSoundResource * SoundResource = NULL;
					JENEW(SoundResource,jeSoundResource(ResourceName))
					SoundMap.set(ResourceName,SoundResource);
					Query->NextRow();
				}
				Query->Drop();
			}
			
			//! Clear Resources
			void ClearResources()
			{
				for(SoundMapType::Iterator Iter = SoundMap.getIterator();!Iter.atEnd();Iter++)
					Iter->getValue()->Drop();
				SoundMap.clear();
			}

			//! Return Resource
			IResource * GetResource(const jeStringc & ResourceName)
			{
				SoundMapType::Node * Node = SoundMap.find(ResourceName);
				if(Node)
					return Node->getValue();
				else
					return NULL;
			}

			//! Return Resource Type
			jeStringc GetResourceType()
			{
				return jeStringc("SoundResource");
			}

			//! Garbage Collector
			void GarbageCollector()
			{
				for(SoundMapType::Iterator Iter = SoundMap.getIterator();!Iter.atEnd();Iter++)
					if(Iter->getValue()->IsNeedToUnload())
						Iter->getValue()->GarbageUnload();
			}
		};
	}
}

#define JE_SOUNDRESOURCE( __Name ) JE_RESOURCEBASE_GET(__Name,SoundResource)


#endif

#endif
